Skip to content

连接服务器 - TcpClientConnect

函数简介

连接到服务器(异步操作,结果通过回调通知)。

接口名称

TcpClientConnect

DLL调用

c
int32_t TcpClientConnect(int64_t instance, int64_t client_handle, const char* host, int32_t port);

参数说明

参数名类型说明
instance长整数型OLAPlug对象的指针,由 CreateCOLAPlugInterFace 接口生成。
client_handle长整数型客户端句柄。
host字符串主机名或IP地址。
port整数型端口号。

示例

SDK 调用

cpp
#include "OLAPlugServer.h"

OLAPlugServer ola;
long client = ola.TcpClientCreate(nullptr, 0, 1);
if (client != 0) {
    // enable_packet_protocol=1 启用分包协议
    // 异步连接,结果在回调 event_type=0/1 通知
    int ret = ola.TcpClientConnect(client, "127.0.0.1", 9000);
    ola.TcpClientDestroy(client);
}
csharp
using OLAPlug;

var ola = new OLAPlugServer();
long client = ola.TcpClientCreate(null, 0, 1);
if (client != 0)
{
    // enable_packet_protocol=1 启用分包协议
    int ret = ola.TcpClientConnect(client, "127.0.0.1", 9000);
    ola.TcpClientDestroy(client);
}
python
from OLAPlugServer import OLAPlugServer

ola = OLAPlugServer()
client = ola.TcpClientCreate(None, 0, 1)
if client:
    # enable_packet_protocol=1 启用分包协议
    ret = ola.TcpClientConnect(client, "127.0.0.1", 9000)
    ola.TcpClientDestroy(client)
java
import com.olaplug.OLAPlugServer;

OLAPlugServer ola = new OLAPlugServer();
long client = ola.TcpClientCreate(null, 0, 1);
if (client != 0) {
    // enable_packet_protocol=1 启用分包协议
    int ret = ola.TcpClientConnect(client, "127.0.0.1", 9000);
    ola.TcpClientDestroy(client);
}
go
import "github.com/ola/olaplug/olaplug"

ola, _ := olaplug.NewOLAPlugServer("OLAPlug_x64.dll")
defer ola.ReleaseObj()
long client = ola.TcpClientCreate(nil, 0, 1);
if (client != 0) {
    // enable_packet_protocol=1 启用分包协议
    ret := ola.TcpClientConnect(client, "127.0.0.1", 9000)
    ola.TcpClientDestroy(client);
}
rust
use olaplug::OLAPlugServer;

let ola = OLAPlugServer::new("OLAPlug_x64.dll").unwrap();
long client = ola.TcpClientCreate(None, 0, 1);
if (client != 0) {
    // enable_packet_protocol=1 启用分包协议
    let ret = ola.tcp_client_connect(&client, "127.0.0.1", 9000);
    ola.TcpClientDestroy(client);
}
cpp
var ola = com("OlaPlug.OlaSoft")
long client = ola.TcpClientCreate(0, 0, 1);
if (client != 0) {
    // enable_packet_protocol=1 启用分包协议
    var ret = ola.TcpClientConnect(client, "127.0.0.1", 9000)
    ola.TcpClientDestroy(client);
}
vbscript
Set ola = CreateObject("OlaPlug.OlaSoft")
client = ola.TcpClientCreate(0, 0, 1)
If client <> 0 Then
    // enable_packet_protocol=1 启用分包协议
    ret = ola.TcpClientConnect(client, "127.0.0.1", 9000)
    ola.TcpClientDestroy(client)
End If
text
.局部变量 ola, OLAPlug
ola.创建 ()
client = ola.TcpClientCreate (0, 0, 1)
.如果真 (client ≠ 0)
    // enable_packet_protocol=1 启用分包协议
    ret = ola.TcpClientConnect (client, "127.0.0.1", 9000)
    ola.TcpClientDestroy (client)
.如果真结束
aardio
import OLAPlugServer;
var ola = OLAPlugServer();
long client = ola.TcpClientCreate(null, 0, 1);
if (client != 0) {
    // enable_packet_protocol=1 启用分包协议
    var ret = ola.TcpClientConnect(client, "127.0.0.1", 9000);
    ola.TcpClientDestroy(client);
}
text
变量 ola <类型 = OLAPlugServer>
ola = 新建 OLAPlugServer
long client = ola.TcpClientCreate(0, 0, 1);
if (client != 0) {
    // enable_packet_protocol=1 启用分包协议
    整数 ret = ola.TcpClientConnect(client, "127.0.0.1", 9000)
    ola.TcpClientDestroy(client);
}
cpp
#include "OLAPlugServer.h"

OLAPlugServer ola;
long client = ola.TcpClientCreate(nullptr, 0, 1);
if (client != 0) {
    // enable_packet_protocol=1 启用分包协议
    int ret = ola.TcpClientConnect(client, "127.0.0.1", 9000);
    ola.TcpClientDestroy(client);
}

原生 DLL 调用

cpp
long instance = CreateCOLAPlugInterFace();
long client = TcpClientCreate(instance, 0, instance, 1);
if (client != 0) {
    int ret = TcpClientConnect(instance, client, "127.0.0.1", 9000);
    // ret==1 表示开始连接
    TcpClientDestroy(instance, client);
}
csharp
using System.Runtime.InteropServices;
using System.Text;

[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long CreateCOLAPlugInterFace();
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long TcpClientCreate(long ola, long callback, long user_data, int enable_packet_protocol);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int TcpClientDestroy(long ola, long client_handle);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int TcpClientConnect(long ola, long client_handle, string host, int port);

long instance = CreateCOLAPlugInterFace();
long client = TcpClientCreate(instance, 0, instance, 1);
if (client != 0) {
    int ret = TcpClientConnect(instance, client, "127.0.0.1", 9000);
    // ret==1 表示开始连接
    TcpClientDestroy(instance, client);
}
python
from ctypes import CDLL, c_int, c_int64, create_string_buffer

ola = CDLL("OLAPlug_x64.dll")
ola.CreateCOLAPlugInterFace.restype = c_int64
instance = ola.CreateCOLAPlugInterFace()
client = ola.TcpClientCreate(instance, 0, instance, 1)
if client:
    int ret = ola.TcpClientConnect(instance, client, "127.0.0.1", 9000);
    // ret==1 表示开始连接
    ola.TcpClientDestroy(instance, client)

返回值

返回值说明
(返回值)整数型,1 成功(开始连接,最终结果通过回调通知),0 失败。

注意事项

项目说明
这是异步操作这是异步操作,函数立即返回。
连接结果连接结果通过回调函数通知(event_type=0 成功,event_type=1 失败)。
支持IPv4和IPv6地址支持IPv4和IPv6地址。
支持域名解析支持域名解析。
如果已经连接如果已经连接,再次调用会失败。